package com.lsz.crm.controller;


import com.lsz.crm.base.BaseController;
import com.lsz.crm.pojo.User;
import com.lsz.crm.service.PermissionService;
import com.lsz.crm.service.UserService;
import com.lsz.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 首页控制器
 *
 * @author lsz
 * @version 1.0
 * @date 2020-12-08 14:24
 */
@Controller
public class IndexController extends BaseController {


    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 登录页面
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    /**
     * 系统欢迎页
     * @return
     */
    @GetMapping("/welcome")
    public String welcome() {
        return "welcome";
    }

    /**
     * 后端管理主页面
     * @return
     */
    @GetMapping("/main")
    public String main(HttpServletRequest request) {
        //查询用户，利用 session 作用域
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        User user = userService.selectByPrimaryKey(userId);
        request.getSession().setAttribute("user",user);

        // 通过用户ID查询当前用户拥有的资源列表（查询对应的授权码）
        List<String> permissions = permissionService.queryUserHasRoleHasPermissionsByRoleId(userId);
        // 将集合设置到session作用域中
        request.getSession().setAttribute("permissions",permissions);
        return "main";
    }

}
